package com.buho29.rcmanager.bd;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

public class TablePowerItem extends BaseActiveRecord  {
	
	public static final String SQL_TABLE_NAME = "powerItem";

	public static final String SQL_CREATE_TABLE = "create table " + SQL_TABLE_NAME + " (" +
				"_id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT," +
	    		"name VARCHAR(20) NOT NULL," +
	    		"description VARCHAR(200) NULL," +
	    		"date INTEGER NOT NULL, " +
			    "capacity INTEGER NOT NULL," +
			    "voltage FLOAT NOT NULL," +
			    "count INTEGER NULL " +
		    ");";
	/*
	 * table column
	 */
	public static final String COL_NAME = "name";
	public static final String COL_DESCRIPTION = "description";
	public static final String COL_DATE = "date";
	public static final String COL_CAPACITY = "capacity";
	public static final String COL_VOLTAGE = "voltage";
	public static final String COL_COUNT = "count";
	
	//public static final String COL_
	public static final String[] COLUMNS = new String[] { 
		COL_ROW_ID, COL_NAME, COL_DESCRIPTION,COL_DATE ,
		COL_CAPACITY , COL_VOLTAGE , COL_COUNT 
	};
	
	private String mName;
	public String getName() {return mName;}
	public void setName(String name) {mName = name;}
	
	private String mDescription;
	public String getDescription() {return mDescription;}
	public void setDescription(String description) {mDescription = description;}
	
	private long mDate;
	public long getDate() {return mDate;}
	
	
	private int mCapacity;
	public int getCapacity() {return mCapacity;}
	public void setCapacity(int capacity) {mCapacity = capacity;}
	
	private float mVoltage;
	public float getVoltage() {return mVoltage;}
	public void setVoltage(float voltage) {mVoltage = voltage;}
	
	private Long mCount;
	public Long getCount() {return mCount;}
	public void setCount(Long count) {mCount = count;}
	
	
	@Override
	public Boolean load() {
		Cursor cursor = mDb.query(true, SQL_TABLE_NAME,COLUMNS,
				COL_ROW_ID + "=" + mId,
				null, null,null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			loadCursor(cursor);
			cursor.close();
			return true;
		}
		return false;
		
	}

	@Override
	public long save() {
		ContentValues values = new ContentValues();
		values.put(COL_NAME,mName);
		values.put(COL_DESCRIPTION,mDescription);
		values.put(COL_CAPACITY,mCapacity);
		values.put(COL_VOLTAGE,mVoltage);
		
		//values.put(COL_,m);
		if (mId < 1) {
			values.put(COL_DATE,new Date().getTime());
			values.put(COL_COUNT,0);
			mId = mDb.insert(SQL_TABLE_NAME, null, values);
			return mId;
		} else { 
			values.put(COL_DATE,mDate);
			values.put(COL_COUNT,mCount);
			return mDb.update(SQL_TABLE_NAME, values, COL_ROW_ID      + "=" + mId, null);
		}
	}
	@Override
	public boolean delete() {
		return mDb.delete(SQL_TABLE_NAME, COL_ROW_ID + "=" + mId,     null) > 0;
	}
	
	@Override
	public Cursor retrieveAll() {
		return mDb.query(SQL_TABLE_NAME, COLUMNS, null, null,
				null, null, null);
	}
	@Override
	public void loadCursor(Cursor cursor) {
		mId = cursor.getLong(cursor.getColumnIndex(COL_ROW_ID));
		mName = cursor.getString(cursor.getColumnIndex(COL_NAME));
		mDescription = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION));
		mDate = cursor.getLong(cursor.getColumnIndex(COL_DATE));
		mCapacity = cursor.getInt(cursor.getColumnIndex(COL_CAPACITY));
		mVoltage = cursor.getFloat(cursor.getColumnIndex(COL_VOLTAGE));
		mCount = cursor.getLong(cursor.getColumnIndex(COL_COUNT));
	}

}
